package com.zhangyuji.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangyuji.common.MyCurrentId;
import com.zhangyuji.common.R;
import com.zhangyuji.entity.Category;
import com.zhangyuji.entity.Dish;
import com.zhangyuji.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @GetMapping("/page")
    public R<IPage> pageQuery(int page, int pageSize) {
        System.out.println(page);
        System.out.println(pageSize);
        IPage<Category> iPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(iPage, queryWrapper);
        return R.success(iPage);
    }

    @DeleteMapping
    public R<String> deleteCategory(Long ids) {
        System.out.println(ids);

        boolean flag = categoryService.removeById(ids);
        if(flag){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @PostMapping
    public R<String> addClassifition(@RequestBody Category category, HttpServletRequest request) {
//        category.setCreateTime(LocalDateTime.now());
//        category.setCreateUser((Long) request.getSession().getAttribute("employee"));
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        MyCurrentId.setCurrentId((Long) request.getSession().getAttribute("employee"));
        System.out.println(category);
        boolean flag = categoryService.save(category);
        System.out.println(flag);
        if (flag) {
            return R.success("添加菜品分类成功!");
        }
        return R.success("添加菜品分类失败!");
    }


    @PutMapping
    public R<String> editCategory(@RequestBody Category category,HttpServletRequest request){
        System.out.println(category);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getId,category.getId());
        Category queryCategory = categoryService.getOne(queryWrapper);
//        queryCategory.setUpdateTime(LocalDateTime.now());
//        queryCategory.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        MyCurrentId.setCurrentId((Long) request.getSession().getAttribute("employee"));
        queryCategory.setSort(category.getSort());
        System.out.println(queryCategory);
        boolean flag = categoryService.updateById(queryCategory);
        if (flag){
            return R.success("修改成功");
        }
        return R.success("修改失败");
    }


    @GetMapping("/list")
    public R<List<Category>> createClassifition(Category category){
        System.out.println(category.getType());
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        List<Category> list = categoryService.list(queryWrapper);
        System.out.println(list);
        return R.success(list);
    }

}
